package cn.eclass.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import cn.eclass.entity.GiftOrderEntity;
import cn.eclass.entity.SchoolOrderEntity;
import cn.eclass.entity.UserEntity;

public class UserSessionInfo {
	//This class obtains user relevant information in the presence of session.

	UserEntity user = null;
	HttpSession session = null;
	int userId = 0;
	public UserSessionInfo(UserEntity user, HttpSession session){
		this.user = user;
		this.session = session;
		userId = user.getId();
	}
	
	public void setSchoolOrderList(){
		String name = ConstantsConf.SCHOOLORDERLIST;
		List<SchoolOrderObject> list = (List<SchoolOrderObject>) session.getAttribute(name);
		if(list!= null) return;
		List<SchoolOrderEntity> soeList = BeanFactory.factory.getSchoolOrderBean().getByUser(userId);
		list = new ArrayList<SchoolOrderObject>();
		int total = 0;
		for(SchoolOrderEntity soe : soeList) {
			SchoolOrderObject soo = new SchoolOrderObject(soe);
			list.add(soo);
			if(soo.isValid()) total += soo.getPoints();
		}
		
		session.setAttribute(ConstantsConf.SCHOOLORDERLIST, list);
		session.setAttribute(ConstantsConf.SCHOOLORDERTOTALPOINTS, total);
	}
	
//	public List<SchoolOrderObject> getSchoolOrderObjectList() {
//		String name = ConstantsConf.SCHOOLORDERLIST;
//		List<SchoolOrderObject> list = (List<SchoolOrderObject>) session.getAttribute(name);
//		if(list!= null) return list;
//		List<SchoolOrderEntity> soeList = BeanFactory.factory.getSchoolOrderBean().getByUser(userId);
//		list = new ArrayList<SchoolOrderObject>();
//		for(SchoolOrderEntity soe : soeList) {
//			SchoolOrderObject soo = new SchoolOrderObject(soe);
//			list.add(soo);
//		}
//		
//		session.setAttribute(name, list);
//		return list;
//	}
//	
//	public int getSchoolOrderTotalPoints() {
//		String name = ConstantsConf.SCHOOLORDERTOTALPOINTS;
//		Integer total = (Integer) session.getAttribute(name);
//		if(total != null) return total;
//		List<SchoolOrderObject> list = (List<SchoolOrderObject>) session.getAttribute(name);
//		if(list == null) list = getSchoolOrderObjectList();
//		total = 0;
//		for(SchoolOrderObject soo : list) {
//			if(soo.isValid()) total += soo.getPoints();
//		}
//		session.setAttribute(name, total);
//		return total;
//	}
	
	public void setGiftOrderList() {
		String name = ConstantsConf.GIFTORDERLIST;
		List<GiftOrderObject> list = (List<GiftOrderObject>) session.getAttribute(name);
		if(list != null) return;
		List<GiftOrderEntity> goeList = BeanFactory.factory.getGiftOrder().getByUser(userId);
		list = new ArrayList<GiftOrderObject>();
		int total = 0;
		for(GiftOrderEntity goe : goeList) {
			GiftOrderObject goo = new GiftOrderObject(goe);
			list.add(goo);
			total += goo.getPoints();
		}
		session.setAttribute(ConstantsConf.GIFTORDERLIST, list);
		session.setAttribute(ConstantsConf.GIFTORDERTOTALPOINTS, total);
	}
	
//	public List<GiftOrderObject> getGiftOrderObjectList() {
//		String name = ConstantsConf.GIFTORDERLIST;
//		List<GiftOrderObject> list = (List<GiftOrderObject>) session.getAttribute(name);
//		if(list != null) return list;
//		List<GiftOrderEntity> goeList = BeanFactory.factory.getGiftOrder().getByUser(userId);
//		list = new ArrayList<GiftOrderObject>();
//		for(GiftOrderEntity goe : goeList) {
//			GiftOrderObject goo = new GiftOrderObject(goe);
//			list.add(goo);
//		}
//		session.setAttribute(name, list);
//		return list;
//	}
//	
//	public int getGiftOrderTotalPoints() {
//		String name = ConstantsConf.GIFTORDERTOTALPOINTS;
//		Integer total = (Integer) session.getAttribute(name);
//		if(total != null) return total;
//		List<GiftOrderObject> list = (List<GiftOrderObject>) session.getAttribute(name);
//		if(list == null) list = getGiftOrderObjectList();
//		total = 0;
//		for(GiftOrderObject goo : list) {
//			total += goo.getPoints();
//		}
//		session.setAttribute(name, total);
//		return total;
//	}
	
}
